package com.jic.interaction.ChinaPost.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import com.jic.interaction.ChinaPost.common.exception.DamagesPayException;
import com.jic.interaction.ChinaPost.common.menu.DamagesPayEnums;
import com.jic.interaction.ChinaPost.service.DamagesPayService;
import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.common.utils.JsonUtil;
import com.jic.interaction.ChinaPost.model.vo.request.CompensationOrderRequest;
import com.jic.interaction.ChinaPost.model.vo.request.DamagesPayRequest;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.core.util.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Service
@Slf4j
public class DamagesPayServiceImpl implements DamagesPayService {

    @Value("${route.value}")
    private String url;

    @Override
    public Long  callCompensationOrder(CompensationOrderRequest compensationOrder) throws InterruptedException, ExecutionException, IOException {
        log.info("赔偿金订单核销接口：请求参数{}", compensationOrder.toString());
        String url = "?";//测试环境
        Map map = JSON.parseObject(JSON.toJSONBytes(compensationOrder),Map.class);
        JSONObject jsonObject = JSONObject.fromObject(compensationOrder);
        String s = HttpClientUtil.requestPath(url, map, JsonUtil.toJson(jsonObject));
        com.alibaba.fastjson.JSONObject jsonObject1 = com.alibaba.fastjson.JSONObject.parseObject(s);
        if (jsonObject1==null || !jsonObject1.containsKey("code")) {
            throw  new DamagesPayException(DamagesPayEnums.RETURN_NOT_NULL);
        }
        String code = jsonObject1.getString("code");
        log.info("赔偿金订单核销结束");
        if (!code.equals("0")){
            throw  new DamagesPayException(jsonObject1.getString("code"),jsonObject1.getString("msg"));
        }
        return 1L;
    }

    @Override
    public Long callDamagesPay(DamagesPayRequest damagesPay) throws InterruptedException, ExecutionException, IOException {
        log.info("赔偿金支付接口开始：请求参数{}", damagesPay.toString());
        String url = this.url+"/m/dbs-front/compensationPay/pay";//测试环境
        //String url = this.url+"/dbs-front/compensationPay/pay";//生产环境

        Map map = JSON.parseObject(JSON.toJSONBytes(damagesPay),Map.class);
        JSONObject jsonObject = JSONObject.fromObject(damagesPay);
        String s = HttpClientUtil.requestPath(url, map, JsonUtil.toJson(jsonObject));
        com.alibaba.fastjson.JSONObject jsonObject1 = com.alibaba.fastjson.JSONObject.parseObject(s);
        if (jsonObject1==null || !jsonObject1.containsKey("code")) {
            throw  new DamagesPayException(DamagesPayEnums.RETURN_NOT_NULL);
        }
        String code = jsonObject1.getString("code");
        log.info("赔偿金订单核销结束");
        if (!code.equals("0")){
            throw  new DamagesPayException(jsonObject1.getString("code"),jsonObject1.getString("msg"));
        }

        log.info("赔偿金支付接口结束");
        return 1L;
    }
}
